package io.github.talelin.latticy.service.impl;

import io.github.talelin.latticy.bo.Student;
import io.github.talelin.latticy.common.util.SnowFlake;
import io.github.talelin.latticy.model.MsgLogDO;
import io.github.talelin.latticy.mq.MessageHelper;
import io.github.talelin.latticy.service.MsgLogService;
import io.github.talelin.latticy.service.StudentService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private MsgLogService msgLogService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void send(Student student) {
        //生成订单号
        SnowFlake idWorker = new SnowFlake(0, 0);
        Long msgId = idWorker.nextId();
        MsgLogDO msgLog = new MsgLogDO(msgId, student, "log.exchange", "log.inbound.operation.queue.key");
        msgLogService.save(msgLog);// 消息入库
        CorrelationData correlationData = new CorrelationData(String.valueOf(msgId));
        rabbitTemplate.convertAndSend("log.exchange","log.inbound.operation.queue.key", MessageHelper.objToMsg(student),correlationData);
    }

}
